package net.lab1024.smartadmin.module.business.gearboxEfficiency.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.dto.GearboxEfficiencyAddDTO;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.dto.GearboxEfficiencyQueryDTO;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.dto.GearboxEfficiencyUpdateDTO;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.entity.GearboxEfficiencyEntity;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.vo.GearboxEfficiencyExcelVO;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.domain.vo.GearboxEfficiencyVO;
import net.lab1024.smartadmin.module.business.gearboxEfficiency.service.GearboxEfficiencyService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * [ 变速箱效率MAP ]
 *
 * @author wuhq
 * @version 1.0
 * @company 变速箱效率MAP
 * @copyright (c)  变速箱效率MAPInc. All rights reserved.
 * @date 2021-11-21 19:18:38
 * @since JDK1.8
 */
@RestController
@Api(tags = {"变速箱效率MAP"})
public class GearboxEfficiencyController extends BaseController {

    @Autowired
    private GearboxEfficiencyService gearboxEfficiencyService;

    @ApiOperation(value = "分页查询变速箱效率MAP",notes = "@author wuhq")
    @PostMapping("/gearboxEfficiency/page/query")
    public ResponseDTO<PageResultDTO<GearboxEfficiencyVO>> queryByPage(@RequestBody GearboxEfficiencyQueryDTO queryDTO) {
        return gearboxEfficiencyService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "添加变速箱效率MAP",notes = "@author wuhq")
    @PostMapping("/gearboxEfficiency/add")
    public ResponseDTO<String> add(@RequestBody @Validated GearboxEfficiencyAddDTO addTO){

        // 保存之前先删除旧的数据
        gearboxEfficiencyService.delByGearboxId(addTO.getGearboxId());

        return gearboxEfficiencyService.add(addTO);
    }

    @ApiOperation(value = "根据变速箱id获取数据",notes = "@author wuhq")
    @GetMapping("/gearboxEfficiency/getByGearboxId/{gearboxId}")
    public ResponseDTO<GearboxEfficiencyEntity> getByGearboxId(@PathVariable Long gearboxId) {
        return ResponseDTO.succData(gearboxEfficiencyService.getByGearboxId(gearboxId));
    }

    @ApiOperation(value="修改变速箱效率MAP",notes = "@author wuhq")
    @PostMapping("/gearboxEfficiency/update")
    public ResponseDTO<String> update(@RequestBody @Validated GearboxEfficiencyUpdateDTO updateDTO){
        return gearboxEfficiencyService.update(updateDTO);
    }

    @ApiOperation(value="批量删除变速箱效率MAP",notes = "@author wuhq")
    @PostMapping("/gearboxEfficiency/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return gearboxEfficiencyService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author wuhq")
    @PostMapping("/gearboxEfficiency/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<GearboxEfficiencyExcelVO> gearboxEfficiencyList = gearboxEfficiencyService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("变速箱效率MAP", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, GearboxEfficiencyExcelVO.class, gearboxEfficiencyList);
        downloadExcel("变速箱效率MAP", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author wuhq")
    @PostMapping("/gearboxEfficiency/export/all")
    public void exportAll(@RequestBody @Validated GearboxEfficiencyQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<GearboxEfficiencyExcelVO> gearboxEfficiencyList = gearboxEfficiencyService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("变速箱效率MAP", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, GearboxEfficiencyExcelVO.class, gearboxEfficiencyList);
        downloadExcel("变速箱效率MAP", workbook, response);
    }

}
